//
//  LoginController.m
//  CaiPiao
//
//  Created by l wb on 2018/5/16.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LoginController.h"

@interface LoginController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *passWordTF;
@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登陆";
    
    
}
- (IBAction)loginBtnClickAction:(UIButton *)sender {
    if (self.userNameTF.text.length == 0) {
        [UIAlertController showAlertWithColorStyle:@{@"headerColor":kNavigationBarColor,@"actionTitleColor":kNavigationBarColor} Title:@"" Message:@"请输入邮箱地址" andDoneAction:nil];
        return;
    } else {
        if (![self  isValidateEmail:self.userNameTF.text]){
            [UIAlertController showAlertWithColorStyle:@{@"headerColor":kNavigationBarColor,@"actionTitleColor":kNavigationBarColor} Title:@"" Message:@"请输入正确的邮箱地址" andDoneAction:nil];
            return;
        }
        
    }
    if (self.passWordTF.text.length < 6||self.passWordTF.text.length >16) {
        [UIAlertController showAlertWithColorStyle:@{@"headerColor":kNavigationBarColor,@"actionTitleColor":kNavigationBarColor} Title:@"" Message:@"请输入6-16位密码" andDoneAction:nil];
        return;
    }
 
    [BmobUser loginInbackgroundWithAccount:self.userNameTF.text andPassword:self.passWordTF.text block:^(BmobUser *user, NSError *error) {
        if (user) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [EasyTextView showErrorText:@"登陆失败"];
        }
    }];
    
    
}
- (IBAction)hiddenPassWordBtn:(UIButton *)sender {
 
    self.passWordTF.secureTextEntry = sender.selected;
    sender.selected = !sender.selected;
    
}
-(BOOL)isValidateEmail:(NSString *)email {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
@end
